<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网站编号" prop="introduceId">
        <el-input
          v-model="queryParams.introduceId"
          placeholder="请输入网站编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信账号" prop="introduceWechat">
        <el-input
          v-model="queryParams.introduceWechat"
          placeholder="请输入微信账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="websiteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="网站编号" align="center" prop="introduceId" />
      <el-table-column label="姓名" align="center" prop="introduceName" />
      <el-table-column label="地址" align="center" prop="introduceAddress" />
      <el-table-column label="QQ账号" align="center" prop="introduceQq" />
      <el-table-column label="微信账号" align="center" prop="introduceWechat" />
      <el-table-column label="电话" align="center" prop="introducePhone" />
      <el-table-column label="内容" align="center" prop="introduceContent" />
      
        <el-table-column
        label="图片"
        align="center"
        prop="introduceImg"
        width="100"
      >
       <template slot-scope="scope">
      <image-preview :src="scope.row.introduceImg" :width="50" :height="50" />
       </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<div align=center style="margin-top:20px">
<el-pagination
text-align:center
  background
  layout="prev, pager, next"
  :total=total
  :page-size=queryParams.pageSize
  :current-page=this.queryParams.pageNum
  @current-change="handleCurrentChange"
    >
</el-pagination>
</div>


    <!-- 添加或修改网站简介对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-col :span="12">
          <el-form-item label="姓名" prop="introduceName">
          <el-input v-model="form.introduceName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="地址" prop="introduceAddress">
          <el-input v-model="form.introduceAddress" placeholder="请输入地址" />
        </el-form-item>
       
        <el-form-item label="QQ账号" prop="introduceQq">
          <el-input v-model="form.introduceQq" placeholder="请输入QQ账号" />
        </el-form-item>
        <el-form-item label="微信账号" prop="introduceWechat">
          <el-input v-model="form.introduceWechat" placeholder="请输入微信账号" />
        </el-form-item>
 </el-col>
 <el-col :span="12">
        <el-form-item label="电话" prop="introducePhone">
          <el-input v-model="form.introducePhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="内容">
           <el-input v-model="form.introduceContent" placeholder="请输入内容" />
        </el-form-item>
     
        <el-form-item label="图片" prop="introduceImg">
        <el-upload
            class="upload-demo"
            action= ""
            :http-request="upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
        </el-form-item>
          </el-col>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWebsite, getWebsite, delWebsite, addWebsite, updateWebsite } from "@/api/website";
import axios from 'axios';
export default {
  name: "Website",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网站简介表格数据
      websiteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        introduceId: null,
        introduceWechat: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网站简介列表 */
    getList() {
      this.loading = true;
      listWebsite(this.queryParams).then(response => {
        this.websiteList =  response.content.data.list;
        this.total = response.content.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        introduceId: null,
        introduceName:null,
        introduceAddress: null,
        introduceQq: null,
        introduceWechat: null,
        introducePhone: null,
        introduceContent: null,
        introduceImg: null,
        createTime: null,
        updateTime: null,
        isDelete: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },//初始化表单
    resetForm(formName) {
    //  this.queryParams.adminName=null;
    //  this.queryParams.adminAccount=null;
    //  this.queryParams.adminPhone=null;
    //   this.$nextTick(() => {
        this.$refs[formName].resetFields();
    //   });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.introduceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网站简介";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const introduceId = row.introduceId || this.ids
      getWebsite(introduceId).then(response => {
        this.form = response.content.data;
        this.open = true;
        this.title = "修改网站简介";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.introduceId != null) {
            updateWebsite(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWebsite(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const introduceIds = row.introduceId || this.ids;
      this.$modal.confirm('是否确认删除网站简介编号为"' + introduceIds + '"的数据项？').then(function() {
        return delWebsite(introduceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
          //改变页码      
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },  
        //图片上传
     handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      upload(param) {
			const formData = new FormData()
			formData.append('fileList', param.file)
			axios.post(process.env.BASE_API+'/city/v1/uploadImage', formData).then(response => {
				//根据返回值进行判断是否上传成功
        console.log(response)
				if(response.data.code==200){
					//上传成功
					console.log('上传图片成功')
          this.fileList=[{name: '当前头像', url: process.env.BASE_API+response.data.content.url}]
          this.form.introduceImg=response.data.content.url
				}else{
					//上传失败
					console.log('图片上传失败')
				}
			}).catch(response => {
				console.log('异常处理')
			})
		}
  }
};
</script>
